#include "StdAfx.h"
#include "IWatershed.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const IWatershed::FunctionNameIWatershed = "Watershed";

IWatershed::IWatershed(void)
{
	_fnApi.SetFunctionName(IWatershed::FunctionNameIWatershed);
	_fnApi.SetFunctionDescription("This function does marker-based image segmentation using watershed algrorithm from a source image");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefString(0, "NULL", "NULL", ProcParamDef::input, 0, 500 ));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


IWatershed::~IWatershed(void)
{

}


bool IWatershed::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		//ProcParamString *WatershedSrc = dynamic_cast<ProcParamString *>(step->GetInputs()->GetParam(0));	//Check the Param number
		//ProcParamInt *result = dynamic_cast<ProcParamInt *>(step->GetOutputs()->GetParam(0));		//Check the type and check the DefType

		cv::Mat output;
		cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());
		//cv::Mat ComparisonImg = cv::imread(WatershedSrc->GetValueStr());
			
		cv::watershed(input, output);

		cv::imwrite(step->GetIntermediateImagePathFilename(), output);

		input.release();
		//result->SetValue(0);
		rc = true;
		
	}
	return rc;
}
